package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.*;
import com.atguigu.result.Result;
import com.atguigu.service.*;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/house")
public class HouseController extends BaseController {
   @Reference
   private HouseService houseService;
    @Reference
   private DictService  dictService;
    @Reference
    private CommunityService  communityService;
    @Reference
    private HouseImageService  houseImageService;
    @Reference
    private HouseBrokerService  houseBrokerService;

    @Reference
    private   UserFollowService userFollowService;

  @RequestMapping("/list/{pageNum}/{pageSize}")
   public Result findListPage(@RequestBody HouseQueryVo houseQueryVo, @PathVariable  Integer pageNum,@PathVariable Integer pageSize){
      PageInfo<HouseVo>  pageInfo = houseService.findListPage(pageNum, pageNum, houseQueryVo);
      return  Result.ok(pageInfo);

  }
     @RequestMapping("info/{id}")
    public Result info(@PathVariable Long id,HttpServletRequest request){
         House  house = houseService.getById(id);
         Community community = communityService.getById(house.getCommunityId());
         List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(id);
         List<HouseImage>  houseImageList = houseImageService.findList(id, 1);
         Map<String,Object>  map=new HashMap<>();
         map.put("house", house);
         map.put("community", community);
         map.put("houseBrokerList",houseBrokerList);
         map.put(" houseImageList",houseImageList);
         boolean  isFollow=false;
           //关注业务后的继续补充

         //符合什么条件 才把isFollw改为true

         // 1必须已经登录了
         //2并且当前用户已经关注
         UserInfo userInfo = (UserInfo) request.getSession().getAttribute("USER");

         if(userInfo!=null){
             isFollow= this.userFollowService.isFollow(id,userInfo.getId());

         }
         map.put("isFollow", isFollow);
         return  Result.ok(map);

     }
}
